package com.awspaass.user.apps.exchangecenterreservation;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListenerInterface;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.controller.MsgNoticeController;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class CateringSendMsgAfterLeave  extends ExecuteListener implements ExecuteListenerInterface {
	@Override
	public String getDescription() {
        return "交流中心餐饮确认给用户发消息！";
    }
	
	@Override
	public void execute(ProcessExecutionContext pec) throws Exception {
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(pec.getTaskInstance(), "预订确认");
		if(flag_ok) {
			try {
				ProcessInstance processInstance = pec.getProcessInstance();
				String bindId = processInstance.getId();//流程实例ID
				String target = pec.getTaskInstance().getTarget();//节点任务办理人
				String telephone =pec.getUserContext().getUserModel().getMobile();//节点办理人电话
				String guanliyuan =pec.getUserContext().getUserModel().getUserName();//办理人名称
				String orderSql = "select id,applyuid,eattingstartdate ,ordersel ,applyname ,applyusercellphone,roomnum,personnum,"
						+ "orderstatus,xscompany,packagestandard,methodpayment,  "
						+ "ycdate  from  VIEW_CHECKRESTAURANT where bindid='"+bindId+"' ";
				List<Map<String, Object>> dataList = DBSql.query(orderSql, new ColumnMapRowMapper(), new Object[] {});
				if (dataList == null || dataList.isEmpty()) {
					return ;
				}
				Map<String, Object> dataMap = dataList.get(0);
				String applyUid = CoreUtil.objToStr(dataMap.get("applyuid"));
				String applyname = CoreUtil.objToStr(dataMap.get("applyname"));//预定人
				String applyusercellphone = CoreUtil.objToStr(dataMap.get("applyusercellphone"));//预定人电话
				String ycdate = CoreUtil.objToStr(dataMap.get("ycdate"));//用餐时间
				String ordersel = CoreUtil.objToStr(dataMap.get("ordersel"));//用餐时段
				String ycst;
				if("0".equals(ordersel)) {//0:午餐|1:晚餐
					 ycst ="午餐";
				}else {
					 ycst ="晚餐";
				}
				String roomnum = CoreUtil.objToStr(dataMap.get("roomnum"));//用餐地点
				String personnum = CoreUtil.objToStr(dataMap.get("personnum"));//用餐人数
				String packagestandard = CoreUtil.objToStr(dataMap.get("packagestandard"));//餐标
				String methodpayment = CoreUtil.objToStr(dataMap.get("methodpayment"));//支付方式
				//发消息
				String msg = "尊敬的"+applyname+"，您好！您已成功预订"+ycdate+"（"+ycst+"）在交流中心["+roomnum+"],用餐人数："
						+personnum+"，餐标："+packagestandard+"，费用："+methodpayment+"，如有任何疑问请及时联系："+guanliyuan+"，电话："+
						telephone+"，恭候您的光临。";
				MsgNoticeController.sendNoticeMsg(UserContext.fromUID(target), msg,"admin", applyUid, "1", "");
				//发短信
				SmsUtil sms = new SmsUtil();
				String param = "{'APPLYNAME':'"+applyname+"','EATTINGSTARTDATE':'"+ycdate+"','ORDERSEL':'"+ycst+"','ROOMNUM':'"+roomnum+"','PERSONNUM':'"+personnum+"','PACKAGESTANDARD':'"+
						packagestandard+"','METHODPAYMENT':'"+methodpayment+"','GUANLIYUAN':'"+guanliyuan+"','TELEPHONE':'"+telephone+"'}";
				SmsUtil.sendSms(applyusercellphone,"SMS_249155844",param);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}
